package cn.ruituo.interceptor;

import cn.ruituo.util.LogUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author oldliu
 * @since 1.0
 */
public class TimeInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         request.setAttribute("start",System.nanoTime());
         return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        long end=System.nanoTime();
        long time=end-(long)request.getAttribute("start");
        LogUtil.info("本次请求："+request.getServletPath()+"耗时："+time+"纳秒");
    }
}
